{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "79b5811c-1074-495f-9722-8325b5e717d3",
   "metadata": {},
   "source": [
    "# Plan-and-Execute\n",
    "\n",
    "This notebook shows how to create a \"plan-and-execute\" style agent. This is heavily inspired by the [Plan-and-Solve](https://arxiv.org/abs/2305.04091) paper as well as the [Baby-AGI](https://github.com/yoheinakajima/babyagi) project.\n",
    "\n",
    "The core idea is to first come up with a multi-step plan, and then go through that plan one item at a time.\n",
    "After accomplishing a particular task, you can then revisit the plan and modify as appropriate.\n",
    "\n",
    "\n",
    "The general computational graph looks like the following:\n",
    "\n",
    "\n",
    "![plan-and-execute diagram](./img/plan-and-execute.png)\n",
    "\n",
    "\n",
    "This compares to a typical [ReAct](https://arxiv.org/abs/2210.03629) style agent where you think one step at a time.\n",
    "The advantages of this \"plan-and-execute\" style agent are:\n",
    "\n",
    "1. Explicit long term planning (which even really strong LLMs can struggle with)\n",
    "2. Ability to use smaller/weaker models for the execution step, only using larger/better models for the planning step\n",
    "\n",
    "\n",
    "The following walkthrough demonstrates how to do so in LangGraph. The resulting agent will leave a trace like the following example: ([link](https://smith.langchain.com/public/d46e24d3-dda6-44d5-9550-b618fca4e0d4/r))."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a44a72d6-7e0c-4478-9d20-4c09000420a8",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, we need to install the packages required."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b451b58a-89bd-424f-8c06-0d9fe325e01b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.11 -m pip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install --quiet -U langchain langchain_openai tavily-python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f267b0-98db-4a59-8b2c-a23f795576ff",
   "metadata": {},
   "source": [
    "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ce438281-08d5-4804-afe7-e4089f7b016b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n",
    "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be2d7981-3737-4134-8bef-d00d18d4e91d",
   "metadata": {},
   "source": [
    "Optionally, we can set API key for LangSmith tracing, which will give us best-in-class observability."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "01f460d1-f26f-47d1-ae76-de74d5d851de",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")\n",
    "os.environ[\"LANGCHAIN_PROJECT\"] = \"Plan-and-execute\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c5fb09a-0311-44c2-b243-d0e80de78902",
   "metadata": {},
   "source": [
    "## Define Tools\n",
    "\n",
    "We will first define the tools we want to use. For this simple example, we will use a built-in search tool via Tavily. However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "25b9ec62-0675-4715-811c-9b32c635b22f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.tools.tavily_search import TavilySearchResults\n",
    "\n",
    "tools = [TavilySearchResults(max_results=3)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dcda478-fa80-4e3e-bb35-0f622fe73a31",
   "metadata": {},
   "source": [
    "## Define our Execution Agent\n",
    "\n",
    "Now we will create the execution agent we want to use to execute tasks. \n",
    "Note that for this example, we will be using the same execution agent for each task, but this doesn't HAVE to be the case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72d233ca-1dbf-4b43-b680-b3bf39e3691f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import create_openai_functions_agent\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "# Get the prompt to use - you can modify this!\n",
    "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
    "# Choose the LLM that will drive the agent\n",
    "llm = ChatOpenAI(model=\"gpt-4-turbo-preview\")\n",
    "# Construct the OpenAI Functions agent\n",
    "agent_runnable = create_openai_functions_agent(llm, tools, prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a3ea9bd3-87d9-4a78-aec6-8ab4bf34479b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.prebuilt import create_agent_executor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "998aebde-c204-494f-930c-14747ed34861",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = create_agent_executor(agent_runnable, tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "746e697a-dec4-4342-a814-9b3456828169",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'who is the winnner of the us open',\n",
       " 'chat_history': [],\n",
       " 'agent_outcome': AgentFinish(return_values={'output': 'The winners of the US Open in 2023 are as follows:\\n\\n- **Golf:** Wyndham Clark won the 2023 US Open in golf, holding his nerve against Rory McIlroy.\\n  \\n- **Tennis:** The 2023 US Open tennis tournament details include information about the event and its prize money, but the winner has not been specified in the provided information. As of the last update, Carlos Alcaraz won the 2022 US Open tennis title.'}, log='The winners of the US Open in 2023 are as follows:\\n\\n- **Golf:** Wyndham Clark won the 2023 US Open in golf, holding his nerve against Rory McIlroy.\\n  \\n- **Tennis:** The 2023 US Open tennis tournament details include information about the event and its prize money, but the winner has not been specified in the provided information. As of the last update, Carlos Alcaraz won the 2022 US Open tennis title.'),\n",
       " 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'US Open winner 2023'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'US Open winner 2023'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"US Open winner 2023\"}', 'name': 'tavily_search_results_json'}})]),\n",
       "   '[{\\'url\\': \\'https://en.wikipedia.org/wiki/2023_U.S._Open_(golf)\\', \\'content\\': \\'Contents 2023 U.S. Open (golf)  was selected to host the 123rd U.S. Open in June 2023. The USGA had made overtures to the club for at least 26 years.  Final round[edit] Sunday, June 18, 2023  Third round[edit] Saturday, June 17, 2023Rory McIlroy falls short as Wyndham Clark holds nerve to win 2023 US Open. The Guardian. Archived from the original on June 19, 2023. Retrieved June 20, 2023.\\'}, {\\'url\\': \\'https://en.wikipedia.org/wiki/2023_US_Open_(tennis)\\', \\'content\\': \"The 2023 US Open is the 143rd consecutive edition of the tournament and will take place at the USTA Billie Jean King  The total overall prize money for the 2023 US Open totals $65 million, 8% more than the 2022 edition.[4]  Contents 2023 US Open (tennis) Wheelchair boys\\' singles Dahnon Ward Wheelchair girls\\' singles Ksénia Chasteau  contract with ESPN, in which the broadcaster holds exclusive rights to the entire tournament and the US Open Series.Carlos Alcaraz defeats Casper Ruud for 2022 US Open title, world No. 1 ranking. US Open. Archived from the original on September 12, 2022. Retrieved September\\\\xa0...\"}]')]}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke(\n",
    "    {\"input\": \"who is the winnner of the us open\", \"chat_history\": []}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cf66804-44b2-4904-b1a7-17ad70b551f5",
   "metadata": {},
   "source": [
    "## Define the State\n",
    "\n",
    "Let's now start by defining the state the track for this agent.\n",
    "\n",
    "First, we will need to track the current plan. Let's represent that as a list of strings.\n",
    "\n",
    "Next, we should track previously executed steps. Let's represent that as a list of tuples (these tuples will contain the step and then the result)\n",
    "\n",
    "Finally, we need to have some state to represent the final response as well as the original input."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8eeeaeea-8f10-4fbe-8e24-4e1a2381a009",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.pydantic_v1 import BaseModel, Field\n",
    "from typing import List, Tuple, Annotated, TypedDict\n",
    "import operator\n",
    "\n",
    "\n",
    "class PlanExecute(TypedDict):\n",
    "    input: str\n",
    "    plan: List[str]\n",
    "    past_steps: Annotated[List[Tuple], operator.add]\n",
    "    response: str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dbd770a-9941-40a9-977e-4d55359eee21",
   "metadata": {},
   "source": [
    "## Planning Step\n",
    "\n",
    "Let's now think about creating the planning step. This will use function calling to create a plan."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4a88626d-6dfd-4488-87f0-a9a0dd6da44c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.pydantic_v1 import BaseModel\n",
    "\n",
    "\n",
    "class Plan(BaseModel):\n",
    "    \"\"\"Plan to follow in future\"\"\"\n",
    "\n",
    "    steps: List[str] = Field(\n",
    "        description=\"different steps to follow, should be in sorted order\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ec7b1867-1ea3-4df3-9a98-992a1c32ec49",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains.openai_functions import create_structured_output_runnable\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "planner_prompt = ChatPromptTemplate.from_template(\n",
    "    \"\"\"For the given objective, come up with a simple step by step plan. \\\n",
    "This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n",
    "The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n",
    "\n",
    "{objective}\"\"\"\n",
    ")\n",
    "planner = create_structured_output_runnable(\n",
    "    Plan, ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0), planner_prompt\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "67ce37b7-e089-479b-bcb8-c3f5d9874613",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Plan(steps=['Identify the current year.', 'Search for the Australia Open winner of the current year.', 'Find the hometown of the identified winner.'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "planner.invoke(\n",
    "    {\"objective\": \"what is the hometown of the current Australia open winner?\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e09ad9d-6f90-4bdc-bb43-b1ce94517c29",
   "metadata": {},
   "source": [
    "## Re-Plan Step\n",
    "\n",
    "Now, let's create a step that re-does the plan based on the result of the previous step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ec2d12cc-016a-44d1-aa08-4c5ce1e8fe2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains.openai_functions import create_openai_fn_runnable\n",
    "\n",
    "\n",
    "class Response(BaseModel):\n",
    "    \"\"\"Response to user.\"\"\"\n",
    "\n",
    "    response: str\n",
    "\n",
    "\n",
    "replanner_prompt = ChatPromptTemplate.from_template(\n",
    "    \"\"\"For the given objective, come up with a simple step by step plan. \\\n",
    "This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n",
    "The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n",
    "\n",
    "Your objective was this:\n",
    "{input}\n",
    "\n",
    "Your original plan was this:\n",
    "{plan}\n",
    "\n",
    "You have currently done the follow steps:\n",
    "{past_steps}\n",
    "\n",
    "Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan. Only add steps to the plan that still NEED to be done. Do not return previously done steps as part of the plan.\"\"\"\n",
    ")\n",
    "\n",
    "\n",
    "replanner = create_openai_fn_runnable(\n",
    "    [Plan, Response],\n",
    "    ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0),\n",
    "    replanner_prompt,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "859abd13-6ba0-45ad-b341-e652dd5f755b",
   "metadata": {},
   "source": [
    "## Create the Graph\n",
    "\n",
    "We can now create the graph!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6c8e0dad-bcea-4c9a-8922-0d820892e2d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "async def execute_step(state: PlanExecute):\n",
    "    task = state[\"plan\"][0]\n",
    "    agent_response = await agent_executor.ainvoke({\"input\": task, \"chat_history\": []})\n",
    "    return {\n",
    "        \"past_steps\": (task, agent_response[\"agent_outcome\"].return_values[\"output\"])\n",
    "    }\n",
    "\n",
    "\n",
    "async def plan_step(state: PlanExecute):\n",
    "    plan = await planner.ainvoke({\"objective\": state[\"input\"]})\n",
    "    return {\"plan\": plan.steps}\n",
    "\n",
    "\n",
    "async def replan_step(state: PlanExecute):\n",
    "    output = await replanner.ainvoke(state)\n",
    "    if isinstance(output, Response):\n",
    "        return {\"response\": output.response}\n",
    "    else:\n",
    "        return {\"plan\": output.steps}\n",
    "\n",
    "\n",
    "def should_end(state: PlanExecute):\n",
    "    if state[\"response\"]:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e954cea0-5ccc-46c2-a27b-f5b7185b597d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.graph import StateGraph, END\n",
    "\n",
    "workflow = StateGraph(PlanExecute)\n",
    "\n",
    "# Add the plan node\n",
    "workflow.add_node(\"planner\", plan_step)\n",
    "\n",
    "# Add the execution step\n",
    "workflow.add_node(\"agent\", execute_step)\n",
    "\n",
    "# Add a replan node\n",
    "workflow.add_node(\"replan\", replan_step)\n",
    "\n",
    "workflow.set_entry_point(\"planner\")\n",
    "\n",
    "# From plan we go to agent\n",
    "workflow.add_edge(\"planner\", \"agent\")\n",
    "\n",
    "# From agent, we replan\n",
    "workflow.add_edge(\"agent\", \"replan\")\n",
    "\n",
    "workflow.add_conditional_edges(\n",
    "    \"replan\",\n",
    "    # Next, we pass in the function that will determine which node is called next.\n",
    "    should_end,\n",
    "    {\n",
    "        # If `tools`, then we call the tool node.\n",
    "        True: END,\n",
    "        False: \"agent\",\n",
    "    },\n",
    ")\n",
    "\n",
    "# Finally, we compile it!\n",
    "# This compiles it into a LangChain Runnable,\n",
    "# meaning you can use it as you would any other runnable\n",
    "app = workflow.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b8ac1f67-e87a-427c-b4f7-44351295b788",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'plan': ['Wait until the 2024 Australian Open concludes.', 'Identify the winner of the 2024 Australian Open.', \"Research the winner's biography to find their hometown.\", 'The hometown of the 2024 Australian Open winner is the result found in the previous step.']}\n",
      "{'past_steps': ('Wait until the 2024 Australian Open concludes.', \"I can't wait for real-time events. However, I can help you find out the schedule, expected dates, or any other information regarding the 2024 Australian Open. How can I assist you further?\")}\n",
      "{'plan': ['Identify the winner of the 2024 Australian Open.', \"Research the winner's biography to find their hometown.\", 'The hometown of the 2024 Australian Open winner is the result found in the previous step.']}\n",
      "{'past_steps': ('Identify the winner of the 2024 Australian Open.', \"The winners of the 2024 Australian Open were Jannik Sinner in the men's singles and Aryna Sabalenka in the women's singles. Jannik Sinner defeated Daniil Medvedev in the final, while specific details about Aryna Sabalenka's match are not provided in the information retrieved.\")}\n",
      "{'plan': [\"Research Jannik Sinner's biography to find his hometown.\", \"Research Aryna Sabalenka's biography to find her hometown.\", 'The hometowns of the 2024 Australian Open winners are the results found in the previous steps.']}\n",
      "{'past_steps': (\"Research Jannik Sinner's biography to find his hometown.\", 'Jannik Sinner was born in San Candido (Innichen), Italy, on August 16, 2001. This is considered his hometown.')}\n",
      "{'plan': [\"Research Aryna Sabalenka's biography to find her hometown.\", 'The hometowns of the 2024 Australian Open winners are the results found in the previous steps.']}\n",
      "{'past_steps': (\"Research Aryna Sabalenka's biography to find her hometown.\", 'Aryna Sabalenka was born in Minsk, the capital of Belarus.')}\n",
      "{'response': 'The hometowns of the 2024 Australian Open winners are San Candido (Innichen), Italy for Jannik Sinner, and Minsk, Belarus for Aryna Sabalenka. No further steps are needed as the final answer has been reached.'}\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "config = {\"recursion_limit\": 50}\n",
    "inputs = {\"input\": \"what is the hometown of the 2024 Australia open winner?\"}\n",
    "async for event in app.astream(inputs, config=config):\n",
    "    for k, v in event.items():\n",
    "        if k != \"__end__\":\n",
    "            print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf585a9-0f1e-4910-bd00-65e7bb05b6e6",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "Congrats on making a plan-and-execute agent! One known limitations of the above design is that each task is still executed in sequence, meaning embarassingly parallel operations all add to the total execution time. You could improve on this by having each task represented as a DAG (similar to LLMCompiler), rather than a regular list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad8f7955-2cc9-4ebb-8c41-13abb3351a24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
